/*
https://leetcode-cn.com/problems/self-crossing/solution/lu-jing-jiao-cha-by-leetcode-solution-dekx/
 */
public class Solution335 {
    public boolean isSelfCrossing(int[] distance) {
        if (distance.length>4){
            if (distance[1]==distance[3]&&distance[4]>=distance[2]-distance[0]){
                return true;
            }
        }
        for (int i=3;i<distance.length;i++){
            if (distance[i]>=distance[i-2]&&distance[i-1]<=distance[i-3]){
                return true;
            }
            if (i>=5&&distance[i-3]-distance[i-5]<=distance[i-1]&&distance[i-1]<=distance[i-3]&&distance[i]>=distance[i-2]-distance[i-4]&&distance[i-2]>distance[i-4]){
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println(new Solution335().isSelfCrossing(new int[]{2,1,1,2}));
    }
}
